import os
import json
import pandas as pd

def convert_json_to_excel(directory):
    # 遍历指定目录下的所有文件
    for filename in os.listdir(directory):
        if filename.endswith('.json'):
            file_path = os.path.join(directory, filename)
            
            # 读取JSON文件
            with open(file_path, 'r', encoding='utf-8') as json_file:
                data = json.load(json_file)
            
            # 将JSON数据转换为DataFrame
            df = pd.DataFrame(data)
            
            # 生成Excel文件名
            excel_filename = os.path.splitext(filename)[0] + '.xlsx'
            excel_path = os.path.join(directory, excel_filename)
            
            # 将DataFrame保存为Excel文件
            df.to_excel(excel_path, index=False)
            
            print(f"已将 {filename} 转换为 {excel_filename}")

# 指定目录路径
directory = './xstw1'

# 调用函数进行转换
convert_json_to_excel(directory)